package com.allwees.bs.module.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.core.modelbase.exception.BusinessException;
import com.allwees.bs.core.modelbase.req.Queryable;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.fms.dao.MerchantWithdrawAccountDao;
import com.allwees.bs.module.fms.entity.MerchantWithdrawAccount;
import com.allwees.bs.module.fms.req.MerchantWithdrawAccountInfoReq;
import com.allwees.bs.module.fms.req.SetDefaultWithdrawReq;
import com.allwees.bs.module.fms.req.query.MerchantWithdrawAccountQueryReq;
import com.allwees.bs.module.fms.service.MerchantWithdrawAccountService;
import com.allwees.core.common.constant.EWithdrawAccountType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 *
 *
 * @author yangylsky
 * @since 2019-12-12
 *
 */
@Service
public class MerchantWithdrawAccountServiceImpl extends BoServiceImpl<MerchantWithdrawAccountDao, MerchantWithdrawAccount>
		implements MerchantWithdrawAccountService {

	@Override
	@Transactional(rollbackFor = Exception.class)
	public MerchantWithdrawAccount createRetailerAccount(MerchantWithdrawAccountInfoReq req) {
		MerchantWithdrawAccount account = req.toEntity();
		if(account.getIsDefault()) {
			_assertNoneDefaultWithdrawAccount(account.getBranchType(), account.getBranchUuid());
		}
		baseMapper.create(account);
		return account;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public MerchantWithdrawAccount createIndividualAccount(MerchantWithdrawAccountInfoReq req) {
		MerchantWithdrawAccount withdrawAccount = req.toEntity();
		if(withdrawAccount.getIsDefault()) {
			_assertNoneDefaultWithdrawAccount(withdrawAccount.getBranchType(), withdrawAccount.getBranchUuid());
		}
		withdrawAccount.setType(EWithdrawAccountType.INDIVIDUAL);
		baseMapper.create(withdrawAccount);
		return withdrawAccount;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public MerchantWithdrawAccount createCompanyAccount(MerchantWithdrawAccountInfoReq req) {
		MerchantWithdrawAccount withdrawAccount = req.toEntity();
		if(withdrawAccount.getIsDefault()) {
			_assertNoneDefaultWithdrawAccount(withdrawAccount.getBranchType(), withdrawAccount.getBranchUuid());
		}
		withdrawAccount.setType(EWithdrawAccountType.COMPANY);
		baseMapper.create(withdrawAccount);
		return withdrawAccount;
	}

	/**
	 * 断言 - 提现账户不存在
	 *
	 * @param branchType
	 * @param branchUuid
	 */
	private void _assertNoneDefaultWithdrawAccount(EBranchType branchType, String branchUuid) {
		MerchantWithdrawAccount defaultWithdrawAccount = findDefaultWithdrawAccount(branchType, branchUuid);
		if(defaultWithdrawAccount != null) {
			throw new BusinessException("商户已存在默认提现账户[" + defaultWithdrawAccount.getBankAccountNo() + "]");
		}
	}

	@Override
	public List<MerchantWithdrawAccount> findAllWithdrawAccount(EBranchType branchType, String branchUuid) {
		return findList(new Queryable<MerchantWithdrawAccount>() {
			@Override
			public QueryWrapper<MerchantWithdrawAccount> toQueryWrapper() {
				return new QueryWrapper<MerchantWithdrawAccount>()
						.eq("branch_type", branchType)
						.eq("branch_uuid", branchUuid);
			}
		});
	}

	@Override
	public MerchantWithdrawAccount findDefaultWithdrawAccount(EBranchType branchType, String branchUuid) {
		return findUnique(new Queryable<MerchantWithdrawAccount>() {
			@Override
			public QueryWrapper<MerchantWithdrawAccount> toQueryWrapper() {
				return new QueryWrapper<MerchantWithdrawAccount>()
						.eq("branch_type", branchType)
						.eq("branch_uuid", branchUuid)
						.eq("is_default", true);
			}
		});
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean setDefaultWithdrawAccount(SetDefaultWithdrawReq req) {
		List<MerchantWithdrawAccount> withdrawAccounts = findAllWithdrawAccount(req.getBranchType(), req.getBranchUuid());
		Assert.notEmpty(withdrawAccounts, "当前用户提现账户为空");
		withdrawAccounts.forEach(withdrawAccount -> {
			if(withdrawAccount.getUuid().equals(req.getWithdrawAccountUuid())) {
				if(!withdrawAccount.getIsDefault()) {
					withdrawAccount.setIsDefault(true);
					updateByUuid(withdrawAccount);
				}
			} else {
				if(withdrawAccount.getIsDefault()) {
					withdrawAccount.setIsDefault(false);
					updateByUuid(withdrawAccount);
				}
			}
		});
		return true;
	}

	@Override
	public IPage<MerchantWithdrawAccount> findPage(Page<MerchantWithdrawAccount> page, MerchantWithdrawAccountQueryReq req) {
		return super.findPage(page, req);
	}
}
